<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager();
need_auth('order');

$t_con = array(
	'begin_time < '.time(),
	'end_time > '.time(),
);
$teams = DB::LimitQuery('team', array('condition'=>$t_con));
$t_id = Utility::GetColumn($teams, 'id');

$condition = array(
	'state' => 1,
);
/* filter */
$uemail = strval($_GET['uemail']);
if ($uemail) {
	$field = strpos($uemail, '@') ? 'email' : 'username';
	//$field = is_numeric($uemail) ? 'id' : $field;
	$uuser = Table::Fetch('user', $uemail, $field);
	
	if($uuser) {
		$oids = DB::LimitQuery('order', array('condition'=>array('user_id' => $uuser['id']) ));
		$t_id = Utility::GetColumn($oids, 'id');
		$o_p_l = Table::Fetch('pin_order_list', $t_id, 'order_id');
		$pin_id_arr = array();
		foreach($o_p_l as $o_p)
		{
			if(empty($pin_id_arr) || !in_array($o_p['pin_id'],$pin_id_arr ) )
			{
				$pin_id_arr[] = $o_p['pin_id'];
			}
		}
		$pin_id_str = implode(',',$pin_id_arr);
		
		
		$condition[] = 'id in ('.$pin_id_str.')';
		
	}
	else $uemail = null;
}
$id = abs(intval($_GET['id'])); 
if ($id){
	$o_p_l = Table::Fetch('pin_order_list', $id, 'order_id');
	$condition['id'] = $o_p_l['pin_id'];
} else 
	$id = '';


$team_id = abs(intval($_GET['team_id']));
if ($team_id && in_array($team_id, $t_id)) {
	//$condition['team_id'] = $team_id;
	
	$oids = DB::LimitQuery('order', array('condition'=>array('team_id' => $team_id) ));
	
	$t_id = Utility::GetColumn($oids, 'id');
	$o_p_l = Table::Fetch('pin_order_list', $t_id, 'order_id');
	$pin_id_arr = array();
	foreach($o_p_l as $o_p)
	{
		if(empty($pin_id_arr) || !in_array($o_p['pin_id'],$pin_id_arr ) )
		{
			$pin_id_arr[] = $o_p['pin_id'];
		}
	}
	$pin_id_str = implode(',',$pin_id_arr);
	
	
	$condition[] = 'id in ('.$pin_id_str.')';
} else { $team_id = null; }

$cbday = strval($_GET['cbday']);
$ceday = strval($_GET['ceday']);
$pbday = strval($_GET['pbday']);
$peday = strval($_GET['peday']);
if ($cbday) { 
	$cbtime = strtotime($cbday);
	$condition[] = "create_time >= '{$cbtime}'";
}
if ($ceday) { 
	$cetime = strtotime($ceday);
	$condition[] = "create_time <= '{$cetime}'";
}
if ($pbday) { 
	$pbtime = strtotime($pbday);
	$condition[] = "pay_time >= '{$pbtime}'";
}
if ($peday) { 
	$petime = strtotime($peday);
	$condition[] = "pay_time <= '{$petime}'";
}
/* end fiter */

$count = Table::Count('pin_order', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$pin_orders = DB::LimitQuery('pin_order', array(
	'condition' => $condition,
	'order' => 'ORDER BY createtime DESC',
	'size' => $pagesize,
	'offset' => $offset,
));

foreach($pin_orders as &$pin_one)
{
	//id	
	$one_pin_orders = DB::LimitQuery('pin_order_list', array(
		'condition' => array('pin_id' => $pin_one['id']),
		'order' => 'ORDER BY createtime asc',
	));
	$pin_one['createtime'] = date('Y-m-d H:i:s',$pin_one['createtime']);
	foreach($one_pin_orders as $tmp_order_one){
		$tp_order = Table::Fetch('order', $tmp_order_one['order_id']);
		//var_dump($tp_order);die();
		if($tp_order['state'] == 'pay')
		{
			$pin_one['tuan_order_id'] = $tmp_order_one['order_id'];
			$tmp_team = Table::Fetch('team', $tp_order['team_id']);
			//title
			$pin_one['tuan_team_title'] = $tmp_team['title'];
			$pin_one['tuan_team_id'] = $tp_order['team_id'];
			
			$pin_one['pin_price'] = $tp_order['money'];
			$tmp_user = Table::Fetch('user', $tp_order['user_id']);
			$pin_one['username'] = $tmp_user['username'];
			break;
		}
	}
}



/**
<th width="40">ID</th>
<th width="180">团长订单id</th>
<th width="340">商品名称</th>
<th width="80" nowrap>拼团价</th>
<th width="100">团长姓名</th>
<th width="80" nowrap>几人团</th>
<th width="80" nowrap>开始时间</th>
<th width="50" nowrap>操作</th>
**/
//id,团长订单id ，商品名称,团长姓名，几人团 开始时间。查看团详情


/**
$pay_ids = Utility::GetColumn($orders, 'pay_id');
$pays = Table::Fetch('pay', $pay_ids);

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);
**/
include template('manage_order_pin');
